﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WCFServiceWebRole1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    
    public class StudentService : IInformation, ITaikhoan
    {
        private string id;
        #region [Information]
        public IEnumerable<Infomation> GetByAllInformation()
        {
            using (var data = new DataClasses1DataContext())
            {

                var result = data.Infomations.ToList();
                //result.ForEach(e => data.Infomations.);
                //var re = (from inf in data.Infomations select inf).ToList();
                return result;
            }
        }
        public bool DeleteIformation(string id)
        {
            using (var data = new DataClasses1DataContext())
            {
                
                try
                {
                    var re = (from inf in data.Infomations where inf.ID == int.Parse(id) select inf).Single();

                    if (re == null) return false;

                    data.Infomations.DeleteOnSubmit(re);
                    data.SubmitChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }


        public bool UpdateInformation(Infomation ifodata)
        {
            try
            {
                using (var data = new DataClasses1DataContext())
                {

                    var re = (from inf in data.Infomations where inf.ID == ifodata.ID select inf).Single();

                    if (re == null) return false;

                    re.Birthday = ifodata.Birthday;
                    re.FirstName = ifodata.FirstName;
                    re.LastName = ifodata.LastName;
                    re.Local = ifodata.Local;
                    re.Phone = ifodata.Phone;
                    data.SubmitChanges();

                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }

        }


        public bool InsertInformation(Infomation ifodata)
        {
            using (var data = new DataClasses1DataContext())
            {

                try
                {
                    Infomation ifo = new Infomation();

                    ifo.Birthday = ifodata.Birthday;
                    ifo.FirstName = ifodata.FirstName;
                    ifo.LastName = ifodata.LastName;
                    ifo.Local = ifodata.Local;
                    ifo.Phone = ifodata.Phone;

                    data.Infomations.InsertOnSubmit(ifo);
                    data.SubmitChanges();
                    return true;
                }
                catch(Exception)
                {
                    return false;
                }
            }
        }
        #endregion


        public IEnumerable<Taikhoan> GetByAllTaikhoan()
        {
            using (var data = new DataClasses2DataContext())
            {
                var result = data.Taikhoans.ToList();
                return result;
            }
        }

        public bool InsertTaikhoan(Taikhoan ifodata)
        {
           using (var data = new DataClasses2DataContext())
           {
                try 
                {
                    Taikhoan ifo = new Taikhoan();

                    ifo.ID = ifodata.ID;
                    ifo.UserName = ifodata.UserName;
                    ifo.Pass = ifodata.Pass;

                    data.Taikhoans.InsertOnSubmit(ifo);
                    data.SubmitChanges();

                    return true;

                }
               catch(Exception)
               {
                    return false;
               }

           }


            
        }

        public bool UpdateTaikhoan(Taikhoan ifodata)
        {
            using (var data = new DataClasses2DataContext())
            {
                try
                {
                    var re = (from inf in data.Taikhoans where inf.ID == ifodata.ID select inf).Single();

                    if (re == null)
                        return false;

                    re.UserName = ifodata.UserName;
                    re.Pass = ifodata.Pass;
                    data.SubmitChanges();
                    return true;
                }

                catch (Exception)
                {
                    return false;
                }
            }
        }

        public bool DeleteTaikhoan(string id)
        {
            using (var data = new DataClasses2DataContext())
            {
                try
                {
                    var re = (from inf in data.Taikhoans where inf.ID == int.Parse(id) select inf).Single();
                    if (re == null)
                        return false;
                    data.Taikhoans.DeleteOnSubmit(re);
                    data.SubmitChanges();

                    return true;

                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
    }
    

    
}
